<?php
namespace app\common\model;

class Turns extends \think\Model
{
    protected $resultSetType = 'collection';

    /**
     * 拉去用户列表时检查倒计时客户轮牌
     */
    public function inserts($loginInfo){
        $higher = $loginInfo -> higher;
        $higherInfo = \app\common\model\Account::where('id',$higher['a_id'])->find();

        if (!in_array($higherInfo['role'],['21','30'])){
            $higher = $higherInfo -> higher;
            $higherInfo = \app\common\model\Account::where('id',$higher['a_id'])->find();
        }

        //获取我所在公司，同样类型的客服
        //获取总监
        $allAccountPre = [];
        $step1[] = ['a_id','eq',$higherInfo['id']];
        $BInfo = \app\common\model\Record::with('BInfo')->where($step1)->select()->toArray();
        $allAccountPre = array_merge($allAccountPre,$BInfo);
        //获取经理
        $step2[] = ['a_id','in',array_column($BInfo,'b_id')];
        $BInfo = \app\common\model\Record::with('BInfo')->where($step2)->select()->toArray();
        $allAccountPre = array_merge($allAccountPre,$BInfo);

        $loginInfo['style'] = array_filter(explode(',',$loginInfo['style']));
        $allAccount = [];

        foreach ($allAccountPre as $value){
            $value['b_info']['style'] = array_filter(explode(',',$value['b_info']['style']));
            if ($value['b_id']!=$loginInfo['id'])
                if (array_intersect($value['b_info']['style'],$loginInfo['style']))
                    if (in_array($value['b_info']['role'],['23','27','22','26']))
                        $allAccount[] = $value['b_id'];
        }


        $allUser = \app\common\model\Message::with('turnsInfo,allTurns,buildingInfo,transferInfo')->where('account_id',$loginInfo['id'])->field('id,status,name')->select();


        //过期用户
        foreach ($allUser as $value){
            if ($value['turns_info'])
                if (!in_array($value['status'],['已定','已买','星标']))
                    if ( time() -$value['turns_info']['date']> 14*24*3600 || $value['status'] =='失效')
                        $this -> hand_out($value,$allAccount);
        }
        return [
            'code' =>'1'
        ];
    }

    //过期用户随机分配
    protected function hand_out($data,$allAccount){
        $data = $data -> toarray();
        $count = count($data['all_turns'])-1;
        //轮牌结束，废弃该用户
        if ($count >= count($allAccount)){
            \app\common\model\Message::where('id', $data['id'])
                ->update([
                    'status' => '失效',
                ]);
            \app\common\model\Message::destroy($data['id']);
        }
        //随机分配该用户
        else{

            $accountPre = array_diff($allAccount,array_column($data['all_turns'],'account_id'));
            $account_id = $accountPre[array_rand($accountPre)];
            $findCheck[] = ['message_id','eq',$data['id']];
            $findCheck[] = ['account_id','eq',$account_id];
            $check = \app\common\model\Turns::where($findCheck)->find();
            if (!$check)
                \app\common\model\Turns::create([
                    'message_id' => $data['id'],
                    'account_id' =>$account_id,
                    'date' => time(),
                ]);

            \app\common\model\Message::where('id', $data['id'])
                ->update([
                    'account_id' => $account_id,
                    'status' => '跟进中',
                    'turns_mark' => '1',
                    'time' => time(),
                    'schedule' => time()
                ]);

            //撤回正在转介的用户
            if ($data['transfer_info'])
                if (!in_array($data['status'],['已定','已买']))
                \app\common\model\Transfer::destroy($data['transfer_info']['id']);

            //发送小程序通知
//            $WxMini =new \app\common\logic\WxMini();
//            $sendData = [
//                'keyword1' => ['value' => $data['name']],
//                'keyword2' => ['value' => $data['telephone']],
//                'keyword3' => ['value' => $data['building_info']['name']],
//                'keyword4' => ['value' => date('Y-m-d H:i:s')],
//                'keyword5' => ['value' => '轮牌用户'],
//            ];
//            $accountInfo = \app\common\model\Account::where('id',$account_id)->find();
//            if ($accountInfo['open_id'])
//                $WxMini -> sendNotify($accountInfo['open_id'],1,'pages/manage/manage',$sendData);
        }

    }
}